# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=pip
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python2-${_realname}" "${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=10.0.1
pkgrel=1
pkgdesc="An easy_install replacement for installing pypi python packages (mingw-w64)"
arch=('any')
license=('MIT')
url="http://www.pip-installer.org/"
depends=("${MINGW_PACKAGE_PREFIX}-python2"
         "${MINGW_PACKAGE_PREFIX}-python2-setuptools"
         "${MINGW_PACKAGE_PREFIX}-python3"
         "${MINGW_PACKAGE_PREFIX}-python3-setuptools")
source=(${_realname}-${pkgver}.tar.gz::https://github.com/pypa/pip/archive/${pkgver}.tar.gz)
noextract=(${_realname}-${pkgver}.tar.gz)
sha256sums=('2c39367b8529f50746b399d7e4563df48a148b8558ae6f7236b51c155359365a')

prepare() {
  cd "$srcdir"/

# work around symlinks in pip package that can cause problems with some building
# where dirs are not removed after being used.
  rm -rf pip-8.1.2/tests/data/packages/symlinks/docs || true 2> /dev/nul
  bsdtar zxf ${_realname}-${pkgver}.tar.gz || true
  for pver in {2,3}; do 
    rm -rf python${pver}-build-${CARCH}| true
    cp -r "${_realname}-${pkgver}" "python${pver}-build-${CARCH}"
  done
}

build() {
  for pver in {2,3}; do
    msg "Python ${pver} build for ${CARCH}" 
    cd "${srcdir}/python${pver}-build-${CARCH}"
    ${MINGW_PREFIX}/bin/python${pver} setup.py build
  done
}

package_python2-pip() {
  depends=("${MINGW_PACKAGE_PREFIX}-python2" "${MINGW_PACKAGE_PREFIX}-python2-setuptools")

  local _mingw_prefix=$(cygpath -am ${MINGW_PREFIX})

  cd "${srcdir}/python2-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    ${MINGW_PREFIX}/bin/python2 setup.py install --prefix=${MINGW_PREFIX#\/} --root="${pkgdir}" --optimize=1 --skip-build

  install -D -m644 LICENSE.txt "${pkgdir}${MINGW_PREFIX}/share/licenses/python2-${_realname}/LICENSE"
  for _ff in ${pkgdir}${MINGW_PREFIX}/bin/*.py; do
    sed -e "s|${_mingw_prefix}|${MINGW_PREFIX}|g" -i ${_ff}
  done
}
package_python3-pip() {
  depends=("${MINGW_PACKAGE_PREFIX}-python3" "${MINGW_PACKAGE_PREFIX}-python3-setuptools")

  local _mingw_prefix=$(cygpath -am ${MINGW_PREFIX})

  cd "${srcdir}/python3-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    ${MINGW_PREFIX}/bin/python3 setup.py install --prefix=${MINGW_PREFIX#\/} --root="${pkgdir}" --optimize=1 --skip-build -v

  # Remove conflicted files
  rm -f ${pkgdir}${MINGW_PREFIX}/bin/pip{.exe,.exe.manifest,-script.py}

  for _ff in ${pkgdir}${MINGW_PREFIX}/bin/*.py; do
    sed -e "s|${_mingw_prefix}|${MINGW_PREFIX}|g" -i ${_ff}
  done

  install -D -m644 LICENSE.txt "${pkgdir}${MINGW_PREFIX}/share/licenses/python3-${_realname}/LICENSE"
}

package_mingw-w64-i686-python2-pip() {
  install=${_realname}2-${CARCH}.install
  package_python2-pip
}

package_mingw-w64-i686-python3-pip() {
  install=${_realname}3-${CARCH}.install
  package_python3-pip
}

package_mingw-w64-x86_64-python2-pip() {
  install=${_realname}2-${CARCH}.install
  package_python2-pip
}

package_mingw-w64-x86_64-python3-pip() {
  install=${_realname}3-${CARCH}.install
  package_python3-pip
}
